package com.sky.server.impl;

import com.sky.dto.DataQueryDTO;
import com.sky.mapper.ReportMapper;
import com.sky.server.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.TurnoverReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service("ReportServiceImpl0")
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;

    @Override
    public TurnoverReportVO turnoverReport(DataQueryDTO dataQueryDTO) {
        TurnoverReportVO turnoverReportVO = new TurnoverReportVO();
        List<String> dateStrings = new ArrayList<>();
        List<BigDecimal> turnovers = new ArrayList<>();
        /*LocalDate beginDate = LocalDate.from(dataOverViewQueryDTO.getBegin());
        LocalDate endDate = LocalDate.from(dataOverViewQueryDTO.getEnd());*/
        LocalDate beginDate = LocalDate.parse(dataQueryDTO.getBegin());
        LocalDate endDate = LocalDate.parse(dataQueryDTO.getEnd());
        while (!beginDate.isAfter(endDate)) {
            dateStrings.add(beginDate.format(DateTimeFormatter.ISO_DATE));
            beginDate = beginDate.plusDays(1);
        }
        for (String dateString : dateStrings) {
            BigDecimal amount = reportMapper.getSumAmount(dateString);
            if (null == amount) {
                amount = BigDecimal.valueOf(0);
            }
            turnovers.add(amount);
        }
        String dateList = String.join(",", dateStrings);
        String turnoverList = String.join(",", turnovers.toString());
        turnoverList = turnoverList.substring(1, turnoverList.length() - 1);
        log.info("********************dateList:{}", dateList);
        log.info("********************dateList:{}", turnoverList);
        turnoverReportVO.setDateList(dateList);
        turnoverReportVO.setTurnoverList(turnoverList);

        return turnoverReportVO;
    }

    @Override
    public OrderReportVO ordersReport(DataQueryDTO dataQueryDTO) {
        OrderReportVO orderReportVO = new OrderReportVO();
        //1 获取dateList 日期
        List<String> dateStrings = new ArrayList<>();
        LocalDate beginDate = LocalDate.parse(dataQueryDTO.getBegin());
        LocalDate endDate = LocalDate.parse(dataQueryDTO.getEnd());
        while (!beginDate.isAfter(endDate)) {
            dateStrings.add(beginDate.format(DateTimeFormatter.ISO_DATE));
            beginDate = beginDate.plusDays(1);
        }
        String dateList = String.join(",", dateStrings);
        orderReportVO.setDateList(dateList);
        //2 获取orderCountList 每日订单数
        List<String> orderCounts = new ArrayList<>();
        dateStrings.forEach(dateString -> {
            Integer orderCount = reportMapper.getOrderCount(dateString);
            orderCounts.add(String.valueOf(orderCount));
        });
        String orderCountList = String.join(",", orderCounts);
        orderReportVO.setOrderCountList(orderCountList);
        //3 获取validOrderCountList 每日有效订单数
        List<String> validOrderCounts = new ArrayList<>();
        dateStrings.forEach(dateString -> {
            Integer orderCount = reportMapper.getValidOrderCount(dateString);
            validOrderCounts.add(String.valueOf(orderCount));
        });
        String validOrderCountList = String.join(",", validOrderCounts);
        orderReportVO.setValidOrderCountList(validOrderCountList);
        //4 获取订单总数 totalOrderCount
        Integer totalOrderCount = reportMapper.getTotalOrderCount(dataQueryDTO);
        log.info("**********获取订单总数:{}", totalOrderCount);
        orderReportVO.setTotalOrderCount(totalOrderCount);
        //5 有效订单数 validOrderCount
        Integer validOrderCount = reportMapper.getTotalValidOrderCount(dataQueryDTO);
        orderReportVO.setValidOrderCount(validOrderCount);
        //6 订单完成率 orderCompletionRate
        BigDecimal orderCompletionRate;
        if (0 == totalOrderCount) {
            orderCompletionRate = BigDecimal.valueOf(0);
        } else {
            orderCompletionRate = BigDecimal.valueOf(validOrderCount).divide(BigDecimal.valueOf(totalOrderCount),2, RoundingMode.HALF_UP);
        }
        orderReportVO.setOrderCompletionRate(orderCompletionRate.doubleValue());
        return orderReportVO;
    }
}
